﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using SuperSocket.WebSocket.Server;
using System;
using System.Collections.Generic;


// WebSocket 本质上是一个持续连接的协议，它支持全双工通信
var builder = WebSocketHostBuilder.Create();

builder.ConfigureAppConfiguration((hostCtx, config) => {
    config.AddInMemoryCollection(new Dictionary<string, string> {
        { "serverOptions:name", "TestServer" },
        { "serverOptions:listeners:0:ip", "Any" },
        { "serverOptions:listeners:0:port", "6018" }
    });
});

 // 消息处理
 builder.UseWebSocketMessageHandler(async (session, msg) =>
 {
     Console.WriteLine($"=>【{session.SessionID}】{session.RemoteEndPoint}【{session.HttpHeader.Method}】{session.Path} {msg.Message}");

     string result;
     switch (session.Path) {
         case "/":
         case "/home":
             result = "欢迎光临";
             break;
         case "/login":
             result = "登录成功";
             break;
         case "/logout":
             result = "退出成功";
             break;
         default:
             result = "Not Found";
             break;
     }
     
     await session.SendAsync(result);
 });

var host = builder.Build();
await host.RunAsync();
